import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import viteCompression from 'vite-plugin-compression';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import ElementPlus from 'unplugin-element-plus/vite'

// https://vitejs.dev/config/
export default (config:{command: 'build' | 'serve',mode:string}) => {
   // 导入生产和开发环境的配置
   process.env = {
    ...process.env,
    ...loadEnv(config.mode, process.cwd())
   };
   return defineConfig({
    plugins: [
          vue(),
          viteCompression(),
          ElementPlus(),
          createSvgIconsPlugin({
            // 指定需要缓存的图标文件夹
            iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
            // 指定symbolId格式
            symbolId: 'icon-[dir]-[name]'
          })
        ],
        resolve: {
          alias: {
            '~': path.resolve(__dirname, './'),
            '@': path.resolve(__dirname, 'src')
          }
        },
        css: {
          preprocessorOptions: {
            less: {
              charset: false,
              javascriptEnabled: true,
              // 这样就能全局使用 src/assets/styles/mixins.less 定义的 变量
              additionalData: `@import "${ path.resolve(__dirname,'src/assets/styles/mixins.less')}";`
            }
          }
        },
        build: {
          rollupOptions: {
            output: {
              assetFileNames: "[hash].[name].[ext]"
            }
          },
          assetsInlineLimit: 4096,
        }
   })
}


